﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Reflection;
using System.Web.Routing;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Core.Validation;

namespace MvcEngine.Mvc.UI
{
    public class ObjectValidatorBuilder : ValidatorBuilderBase
    {
        protected readonly string formName;

        public ObjectValidatorBuilder(string formName)
        {
            this.formName = formName;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder regexBuilder = new StringBuilder();
            StringBuilder rulesBuilder = new StringBuilder();
            StringBuilder messageBuilder = new StringBuilder();

            builder.AppendLine("<script type=\"text/javascript\">");
            builder.AppendLine("$().ready(function() {");

            rulesBuilder.AppendLine("rules: {");
            messageBuilder.AppendLine("messages: {");

            foreach (var item in Items)
            {
                rulesBuilder.Append(item.Name);
                rulesBuilder.Append(": {");

                messageBuilder.Append(item.Name);
                messageBuilder.Append(": {");

                foreach (var val in item.Validators)
                {
                    if (val is RequiredFieldValidatorAttribute)
                    {
                        rulesBuilder.Append("required : true");
                        messageBuilder.Append("required : \"" + val.ErrorMessage + "\"");
                    }
                    else if (val is RegularExpressionValidatorAttribute)
                    {
                        rulesBuilder.Append(item.Name + "Val: true");
                        messageBuilder.Append(item.Name + "Val : \"" + val.ErrorMessage + "\"");
                        regexBuilder.AppendLine("$.validator.addMethod(\"" + item.Name + "Val\", function (value) {");
                        regexBuilder.AppendLine("return /" + ((RegularExpressionValidatorAttribute)val).Expression + "/.test(value);");
                        regexBuilder.AppendLine("});");
                    }
                    else if (val is LengthValidatorAttribute)
                    {
                        rulesBuilder.Append("minlength : " + ((LengthValidatorAttribute)val).MinLength);
                        messageBuilder.Append("minlength : \"" + val.ErrorMessage + "\"");
                    }

                    rulesBuilder.Append(",");
                    messageBuilder.Append(",");
                }
                rulesBuilder.Remove(rulesBuilder.Length - 1, 1);
                messageBuilder.Remove(messageBuilder.Length - 1, 1);

                rulesBuilder.Append(" },");
                messageBuilder.Append(" },");
            }

            rulesBuilder.Remove(rulesBuilder.Length - 1, 1);
            messageBuilder.Remove(messageBuilder.Length - 1, 1);
            rulesBuilder.AppendLine("},");
            messageBuilder.AppendLine("}");

            builder.AppendLine(regexBuilder.ToString());
            builder.AppendLine("$(\"#" + formName + "\").validate({");

            builder.AppendLine("invalidHandler: function(form, validator) {");
            builder.AppendLine("$(\".field-validation-error\").remove();");
            builder.AppendLine("},");

            builder.AppendLine("errorElement: \"span\",");
            builder.AppendLine(rulesBuilder.ToString());
            builder.AppendLine(messageBuilder.ToString());
            builder.AppendLine("});});");
            builder.AppendLine("</script>");

            return builder.ToString();
        }
    }
}
